package models

import (
	_ "time"

	orm "go-papers/database"
	"go-papers/tools"
)

type OrderOprateLog struct {
	OrderLogId      int    `json:"orderLogId" gorm:"type:int(11) unsigned;primary_key"` //
	OrderId         int    `json:"orderId" gorm:"type:int(11);"`                        // 订单id
	Title           string `json:"title" gorm:"type:varchar(255);"`                     // 操作标题
	Desc            string `json:"desc" gorm:"type:varchar(255);"`                      // 操作详情
	LogType         string `json:"logType" gorm:"type:varchar(16);"`                    // 操作类型  1 后台人员 ，3 系统自动，  5 会员
	OrderStatusName string `json:"orderStatusName" gorm:"type:varchar(255);"`           // 订单状态
	PayStatusName   string `json:"payStatusName" gorm:"type:varchar(255);"`             // 支付状态
	LogName         string `json:"logName" gorm:"type:varchar(255);"`                   // 操作人
	LogUid          int    `json:"logUid" gorm:"type:int(11);"`                         // 对应类型 uid
	DataScope       string `json:"dataScope" gorm:"-"`
	Params          string `json:"params"  gorm:"-"`
	BaseModel
}

func (OrderOprateLog) TableName() string {
	return "order_oprate_log"
}

// 创建OrderOprateLog
func (e *OrderOprateLog) Create() (OrderOprateLog, error) {
	var doc OrderOprateLog
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取OrderOprateLog
func (e *OrderOprateLog) Get() (OrderOprateLog, error) {
	var doc OrderOprateLog
	table := orm.Eloquent.Table(e.TableName())
	if e.OrderLogId != 0 {
		table = table.Where("order_log_id = ?", e.OrderLogId)
	}
	if e.OrderId != 0 {
		table = table.Where("order_id = ?", e.OrderId)
	}
	if e.Title != "" {
		table = table.Where("title = ?", e.Title)
	}
	if e.Desc != "" {
		table = table.Where("desc = ?", e.Desc)
	}
	if e.LogType != "" {
		table = table.Where("log_type = ?", e.LogType)
	}
	if e.OrderStatusName != "" {
		table = table.Where("order_status_name = ?", e.OrderStatusName)
	}
	if e.PayStatusName != "" {
		table = table.Where("pay_status_name = ?", e.PayStatusName)
	}
	if e.LogUid != 0 {
		table = table.Where("log_uid = ?", e.LogUid)
	}
	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取OrderPay
func (e *OrderOprateLog) GetList() []OrderOprateLog {
	var doc []OrderOprateLog
	table := orm.Eloquent.Select("*").Table(e.TableName())

	if e.OrderId <= 0 {
		e.OrderId = -1
	}
	table = table.Where("order_id = ?", e.OrderId)
	if err := table.Find(&doc).Error; err != nil {
		return nil
	}
	return doc
}

// 获取OrderOprateLog带分页
func (e *OrderOprateLog) GetPage(pageSize int, pageIndex int) ([]OrderOprateLog, int, error) {
	var doc []OrderOprateLog

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.OrderLogId != 0 {
		table = table.Where("order_log_id = ?", e.OrderLogId)
	}
	if e.OrderId != 0 {
		table = table.Where("order_id = ?", e.OrderId)
	}
	if e.Title != "" {
		table = table.Where("title = ?", e.Title)
	}
	if e.Desc != "" {
		table = table.Where("desc = ?", e.Desc)
	}
	if e.LogType != "" {
		table = table.Where("log_type = ?", e.LogType)
	}
	if e.OrderStatusName != "" {
		table = table.Where("order_status_name = ?", e.OrderStatusName)
	}
	if e.PayStatusName != "" {
		table = table.Where("pay_status_name = ?", e.PayStatusName)
	}
	if e.LogUid != 0 {
		table = table.Where("log_uid = ?", e.LogUid)
	}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新OrderOprateLog
func (e *OrderOprateLog) Update(id int) (update OrderOprateLog, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_log_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除OrderOprateLog
func (e *OrderOprateLog) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_log_id = ?", id).Delete(&OrderOprateLog{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *OrderOprateLog) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_log_id in (?)", id).Delete(&OrderOprateLog{}).Error; err != nil {
		return
	}
	Result = true
	return
}
